package za.co.yellowfire.solarflare.web.model;

import org.hibernate.validator.constraints.Email;
import za.co.yellowfire.solarflare.annotation.Searchable;
import za.co.yellowfire.solarflare.annotation.SearchableProperty;
import za.co.yellowfire.solarflare.annotation.SearchablePropertyId;

import javax.persistence.*;
import javax.validation.constraints.Past;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import java.util.Date;

/**
 * @author Mark P Ashworth
 * @version 0.0.1
 */
@Searchable(name = "PersonalContact")
@Entity(name = "PersonalContact")
@Access(AccessType.FIELD)
@Table(name = "personal_contact", catalog = "slr")
@NamedQueries({
        @NamedQuery(
            name="qry.contacts",
            query="select p from PersonalContact p"
        )
})
@XmlType(name = "contact", propOrder = {"id", "name", "email"})
public class PersonalContact extends DomainEntity {
    private static final long serialVersionUID = 1L;

    public static final String QRY_CONTACTS = "qry.contacts";

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "personal_contact_id", nullable = false, insertable = true, updatable = true)
    @XmlAttribute(name = "id", required = false)
    @SearchablePropertyId
    private Long id;

    @Embedded
    @SearchableProperty
    private Name name = new Name();

    @Past(message = "The birth date must be in the past")
    //@XmlJavaTypeAdapter(value=DateTypeAdapter.class)
    @Temporal(TemporalType.DATE)
    @Column(name = "dob", nullable = false, insertable = true, updatable = true)
    @XmlAttribute(name = "dob", required = true)
    @SearchableProperty(name = "birthDate", dynamic = true)
    private Date birthDate;

    @Basic @Email
    @Column(name = "email")
    @XmlAttribute(name = "email", required = true)
    @SearchableProperty
    private String email;

    @Version
    @Column(name = "version")
    @SuppressWarnings("unused")
    private int version;

    /**
     * The persistent identity of the domain object
     * @return The persistent identity
     */
    @Override
    public Long getId() {
        return id;
    }

    /**
     * The name of the contact
     * @return The name
     */
    public Name getName() {
        return name;
    }

    /**
     * The name of the contact
     * @param name The new name
     */
    public void setName(Name name) {
        this.name = name;
    }

    /**
     * The date of birth of the contact
     * @return The date of birth
     */
    public Date getBirthDate() {
        return birthDate;
    }

    /**
     * The date of birth of the contact
     * @param birthDate The new date of birth
     */
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * The email address of the contact
     * @return The email address
     */
    public String getEmail() {
        return email;
    }

    /**
     * The email address of the contact
     * @param email The new email address
     */
    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PersonalContact)) return false;

        PersonalContact contact = (PersonalContact) o;

        if (id != null ? !id.equals(contact.id) : contact.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "PersonalContact{" +
                "id=" + id +
                ", name=" + name +
                ", birthDate=" + birthDate +
                ", email='" + email + '\'' +
                ", version=" + version +
                '}';
    }
}
